package com.ljy.jymail.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.ljy.jymail.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

//@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public  String app_id = "2021000119616410";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCt7FgNDQx96RpUoYMHK8eAnzghTEPoQWGZwhxnuGXWXTVLyW/82YfDW5uNbyrnwXmYz3lg9HLuNOlM/c5+58pE2FXbGLh1qE6NgfOOhAISlYeC45ilYwpCuh0e3UozpNeg0jM0lT6U2qtwhJFC+5EIDlOdh7dfbpCqYWq3DQQCASc3WjpnoYJkMtVA+iu4AnuSy7d1uh3VUyqr4XOJlqsumPpNx1Pd+oyuiNzTaxjJlvwQ4VExM8s9c2LMLh2LFV2zPknp7WATaPQcbsxZfMfTaAnBCeikceROyPrMccScdhx686tF3sIf0YjXFvFec7JduQbF77HUpmhds1KZrfyRAgMBAAECggEBAJ/++O1goKo/w2IxhnkVVB1F23v+TcYeR2wMPtdHsh9mdX33nFbtwk/8OPf+WWkD+hmSW2XO2t+5uxmBrvHdx5xXq3u3nRFXuJPDBUyIseNovVWyjB0xJCJIVjurdPRXY6nlVQe9d+m109rZiUwmYsAXdeJIFG/tJZYnseUdSx+PbZGsIeU6qP6ltWZks+WmUBc/dzqOCom3dN4q3xbNfQulhNKDesor9l+U5Y04cMoxKaP9/zfwVeelWswIwNR6rlgnJVG8rf6WqNa5kp7jwOEI5Nwkc/o7ykhP3DPI0YdDPBSiXt3fN03czoc2VsHLzPnwT6j4EkjxNACpX5CldrUCgYEA2khHbMBlOVJWuzaMWT5QmQlFmtDGppqFkUpteTZd7OUsteyNbNCHxtcq5uxFKR9KRgT+2kuueswXR1KOzLKlHXdvSj52QvttddflEheLZATicDgHPECWoaZqC3y8bZBYzwNGopJc3xjFzzcoVlWjogcpTrXiK4CiZ/Taikd1duMCgYEAy/nWwD8WE7koWc0sNEXOI+waA5VF2smXMJ6jSb5QHo5wQJePOG7Fycm23OwExNtBJBHFe66sn0HCNbx8MRMFdZnKzkHQs4+24Hj378Qz0734mbRzvs2QDsgBKMOfFZQvdlM6juStnRRNUJCYZRTaRuUEM+2GFQfYUAJ5JfeTpPsCgYAco/UzML9XWRVmNQ8POpzqp4vJd3FJNOcnnCNnDxod1igaqrIXv/jAv0Syzfd5INDnBQrHa7y2Agdf5VY1R49goWYp0K5FtaW0G6LDz/ELK5TmcoOIhVT+Ws55OWGj4pe0edKPzDKBV0fFG5CgW1ZAET57RGQrnxIwH7F5CU1BTwKBgAJr4trBZPey+QGn5+Bu+7V9dgmY2g1w2zSejX2wnq7eLgL2SaL33a05kcwpmRzlJW7SLy/6sN3rH+0XYboLacMtojS0n0+iYx9jED5ua9cVqD/a3ozjT9R9F1ZujBLrgWy8LDPKDy2QMiLEtKpuhvDUgypCpZbWUwEQJnQG7l/FAoGAKewq3eC5Tpwowi5+UVepd9SuY6DMp6mnQMV3qu8BWMO6No+kdYe0SyxzBcwp8RtIwWRwyTnxyT79/GczGwAEb+x7RtL6MW+eQEiIy/2WZUzlUQKc1varHRjmK7g7wx4DNySiwIFT4CMNEI3crd4ZTLOYJw2hKglnlU75FyVYOEA=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo6E1PQ52GdSxvBftqoKDMPfeY2kN+pk3PY2/3puPZGwHAmwqPgStfExOl/JwCZSs8eBIOyg/KyQR/eUh1npIgTAUwtehqJnEirJhFQY5sR2y700MZYzRHSrLF4hMa5OhuTsewLx7vSrcxVmGzRpkidCYWpBa1ypFaOXRc2LKmJXWjLPQgjwjyFgGQwSuCfSiL7xcsFMO6J5dLbfLRpAhx7OLPpxtbDGc2yZwHsTUBSk456wc6wycRZBQ8tAilE9lhtY/n9p4YVTCbgqlnH/1uY91gZmsqAxekQJsunqQcx2PDcI7za2BUa/zkWfxIEYcoQCVMaOZjm8Pb9vqEUMW/wIDAQAB";

    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public  String notify_url = "https://48u4939222.eicp.vip/order/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public  String return_url = "https://48u4939222.eicp.vip/member/memberOrder.html";

    // 签名方式
    public  String sign_type = "RSA2";

    public String timeout = "30m";

    // 字符编码格式
    public  String charset = "utf-8";

    // 支付宝网关
    public  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    // 日志路径
//    public static String log_path = "C:\\";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                //支付宝自动收单，一段时间不支付就不能支付了
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
